const { getping } = require("../../http/api");
const api = require("../../http/api")

// pages/xq/xq.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
       id:'',
       active:0,
       pics:[],
       basicInfo:{},
       content:"",
       show:false,
       show1:false,
       properties:[],
       tabsHeight:0,
       jian0:0,
       xiang1:0,
       tabs:["jian","xiang"],
       done : false,
       xqall:{}
    },
    guige(e){
     const propertyindex = e.currentTarget.dataset.propertyindex
     const propertychildindex = e.currentTarget.dataset.propertychildindex
 
     const property = this.data.xqall.properties[propertyindex]
     const child = property.childsCurGoods[propertychildindex]
     const _childActive = child.active
     console.log(_childActive)
    },
    scroll(){
     //     console.log('3123123123',this.data.done);
         if(this.data.done) return
        var query = wx.createSelectorQuery();
        query.select('#tabss').boundingClientRect((rect) => {
             var tabsHeight = rect.height
             this.setData({
                  tabsHeight: tabsHeight
             })
        }).exec()
        // 获取每个盒子的高度
        this.data.tabs.forEach((item, index) => {
             let name = item + `${index}`
             item = "#" + item
             var query = wx.createSelectorQuery();
             query.select(item).boundingClientRect((rect) => {
                  var itemTop = rect.top
                  if (name == 'jian0') {
                       this.setData({
                            jian0: itemTop
                       })
                  } else if (name == 'xiang1') {
                       this.setData({
                          xiang1: itemTop
                       })
                  }
             }).exec()
        })
        let box0 = this.data.jian0
        let box1 = this.data.xiang1
        let tabGao = this.data.tabsHeight
        if(box0-tabGao<=0&&0<box1-tabGao){
             this.setData({
                  active : 0
             })
        }else{
             this.setData({
                  active : 1
             })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    huan(e){
        console.log(e)
        const index=e.detail.index
        this.setData({
             idarr:this.data.tabs[index],
             done : true
        })
        setTimeout(() => {
             this.setData({
                  done : false
             })
        }, 500);
    },
    onClose() {
        this.setData({ show: false });
      },
  
    addgwc(){
        this.setData({ show: true });
    },
    onLoad: function (options) {
       console.log(options)
       this.setData({
           id:options.id
       })
    },
   async get(){
     const res=await api.getxq(this.data.id)
     console.log(res)
     res.data.content=  res.data.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;float:left;display:block" ')
     .replace(/<section/g, '<view')
    .replace(/\/section>/g, '\view>')
    
     const {pics,basicInfo,content,properties}=res.data
     const goods=res.data
     goods.basicInfo.num=1
     this.setData({
          goods,
          selectSizePrice: goods.basicInfo.minPrice,
          selectSizeOPrice: goods.basicInfo.originalPrice,
        pics:pics,
        basicInfo:basicInfo,
        content:content,
        properties:properties
     })
   },
   guige(e){
     //  console.log(e)
      const pid = e.target.dataset.pid
      const id = e.target.dataset.id
      const cont=this.data.goods.properties.find(item=>{
        return item.id==pid
      })
      cont.childsCurGoods.forEach(item=>{
           if(item.id==id){
                item.active=true
           }else{
               item.active=false
           }
      })
     //  console.log(cont)
      this.setData({
          properties: this.data.goods.properties
        })
     this.getjiage()
   },
   async getjiage(){
     let price = this.data.goods.basicInfo.minPrice
     let originalPrice = this.data.goods.basicInfo.originalPrice
   
     let  stores= this.data.goods.basicInfo.stores
  
 
       const guigenum=this.data.goods.properties.length
       let xuannum=0
       let propertyChildIds=""
       // let childname=""
      this.data.goods.properties.forEach(item=>{
        item.childsCurGoods.forEach(it=>{
           if(it.active){
             xuannum++
             propertyChildIds=propertyChildIds+item.id+":"+it.id+','
           }
           // console.log('9999',propertyChildIds)
        })
      })
      let jiage=false
      if(xuannum==guigenum){
       jiage=true
      }
      const token=wx.getStorageSync('token')
      const res=await api.getgoodjiage({
          token:token?'token':'',
          goodsId:this.data.goods.basicInfo.id,
          propertyChildIds:propertyChildIds
      })
      console.log('jiage',res)
      if(res.code==0){
         price = res.data.price
         originalPrice = res.data.originalPrice
      }
      this.setData({
       selectSizePrice: price,
       selectSizeOPrice: originalPrice,
       stores,
     });
     // console.log('500',price)
     // console.log('562',this.data.selectSizePrice)
     // console.log(this.data.goods.basicInfo)
   },
   jia(){
     const goods=this.data.goods
     // console.log(goods)
     if (goods.basicInfo.num < goods.basicInfo.stores) {
          goods.basicInfo.num++
          this.setData({
            goods
          })
        }
        console.log(this.data.goods)
   },
   jian(){
     const goods = this.data.goods
    if (goods.basicInfo.num >1) {
      goods.basicInfo.num--
      this.setData({
        goods
      })
    }
   },
   btnaddgwc(){
        const goods=this.data.goods
        console.log(goods)
        if(goods.properties){
          const guigelen=goods.properties.length
          const sku=[]
          goods.properties.forEach(item=>{
               const iss=item.childsCurGoods.find(it=>{
                    return it.active
               })
               if(!iss){
                    return
               }
               console.log(item.childsCurGoods)
               sku.push({
                optionId: iss.propertyId,
                optionValueId: iss.id
               })
               console.log(sku)
          })
          if(sku.length!==guigelen){
               wx.showToast({
                 title: '请选择规格',
                 icon:'none'
               })
               return
             }
             const arr = {
               goodsId: goods.basicInfo.id,
               number: goods.basicInfo.num,
               sku:JSON.stringify(sku)
             }
             this.addGWC(arr)
        }else{
          const arr = {
               goodsId: goods.basicInfo.id,
               number: goods.basicInfo.num,
             }
             this.addgwcno(arr)
        }
        wx.showToast({
          title: '加入成功',
          icon: 'success'
        })
        this.setData({
          properties:null,
          show:false
        })
   },
  async addGWC(arr){
     const res=await api.getaddgwc({token:wx.getStorageSync('token'),sku:arr.sku,number:arr.number,goodsId:arr.goodsId})
     console.log(res)
   },
   async addgwcno(arr){
     const res=await api.getaddgwcno({token:wx.getStorageSync('token'),number:arr.number,goodsId:arr.goodsId})
     console.log(res)
   },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.get()
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})